﻿package com.tudou.track
{
    import com.tudou.ad.interfaces.*;
    import com.tudou.config.*;
    import com.tudou.fast.*;
    import com.tudou.model.*;
    import com.tudou.newPlayer.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class AOPTracker extends Object
    {
        protected var nodeLog:Array;
        protected var playerStatus:IPlayerStatus;
        protected var isSample:Boolean = false;
        protected var sendTimer:Timer;
        protected var baseInfo:IBaseInfo;
        static var _pscnt:int = 1;
        static var _prcnt:int = 1;
        private static var lock:Boolean = true;
        private static var instance:AOPTracker = null;
        static var _pcnt:int = 1;
        static var _adcnt:int = 1;

        public function AOPTracker()
        {
            if (!lock)
            {
            }
            else
            {
                throw new Error("This is a singleton class AOPTracker!");
            }
            return;
        }// end function

        protected function changeStreamHlr(event:PlayerEvent) : void
        {
            return;
        }// end function

        public function startSendLog() : void
        {
            if (!isSample)
            {
                return;
            }
            if (sendTimer == null)
            {
                sendTimer = new Timer(5000);
                sendTimer.addEventListener(TimerEvent.TIMER, sendTimerHlr);
            }
            sendTimer.reset();
            _pcnt = 0;
            _pscnt = 0;
            _adcnt = 0;
            _prcnt = 0;
            return;
        }// end function

        protected function continueAop() : void
        {
            sendTimer.start();
            return;
        }// end function

        protected function pauseAop() : void
        {
            sendTimer.stop();
            return;
        }// end function

        public function addAdNodeLog(param1:String, param2:int, param3:Array = null) : void
        {
            if (!isSample)
            {
                return;
            }
            var _loc_4:Array = [];
            if (param3 != null)
            {
                _loc_4 = _loc_4.concat(param3);
            }
            _loc_4.unshift(param2);
            _loc_4.unshift(param1);
            _loc_4.push(new Date().time);
            sendLog(_loc_4.join("|"), 2);
            return;
        }// end function

        public function init(param1:IBaseInfo, param2:IPlayerStatus, param3:Boolean) : void
        {
            if (!param3)
            {
                return;
            }
            this.baseInfo = param1;
            this.isSample = param3;
            this.playerStatus = param2;
            if (param2 != null)
            {
                this.playerStatus.addEventListener(PlayerEvent.CHANGE_VIDEO_STATUS, changeVideoStatusHlr);
                this.playerStatus.addEventListener(PlayerEvent.CHANGE_STREAM, changeStreamHlr);
            }
            return;
        }// end function

        protected function changeVideoStatusHlr(event:PlayerEvent) : void
        {
            if (!playerStatus)
            {
                return;
            }
            switch(playerStatus.videoStatus.status)
            {
                case VideoStatus.PLAYING:
                {
                    addNodeLog(NodeLog.PLAYER_PLAYING);
                    continueAop();
                    break;
                }
                case VideoStatus.PAUSED:
                {
                    addNodeLog(NodeLog.PLAYER_PAUSED);
                    pauseAop();
                    break;
                }
                case VideoStatus.COMPLETED:
                {
                    addNodeLog(NodeLog.VIDEO_PLAY_COMPLETE);
                    pauseAop();
                    break;
                }
                case VideoStatus.BUFFERING:
                {
                    addNodeLog(NodeLog.VIDEO_BUFFERING);
                    pauseAop();
                    break;
                }
                case VideoStatus.READY:
                {
                    continueAop();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function addAdNewNodeLog(param1:String, param2:IAdItemInfo, param3:Array = null) : void
        {
            if (!isSample)
            {
                return;
            }
            sendLog(getAdNewNodeLog(param1, param2, param3), 2);
            return;
        }// end function

        protected function sendTimerHlr(event:Event) : void
        {
            if (!playerStatus)
            {
                return;
            }
            var _loc_2:Array = [];
            _loc_2.push(NodeLog.PLAY_INFO);
            _loc_2.push(playerStatus.startTime.toFixed(2));
            _loc_2.push(playerStatus.playTime.toFixed(2));
            _loc_2.push(playerStatus.loadTime.toFixed(2));
            _loc_2.push(playerStatus.totalTime.toFixed(2));
            _loc_2.push(baseInfo.currRateType);
            _loc_2.push(new Date().time);
            sendLog(_loc_2.join("|"), 3);
            return;
        }// end function

        public function addNodeLog(param1:String, param2:Array = null) : void
        {
            if (!isSample)
            {
                return;
            }
            var _loc_3:Array = [];
            if (param2 != null)
            {
                _loc_3 = _loc_3.concat(param2);
            }
            _loc_3.unshift(param1);
            _loc_3.push(new Date().time);
            sendLog(_loc_3.join("|"), 1);
            return;
        }// end function

        public function getAdNewNodeLog(param1:String, param2:IAdItemInfo, param3:Array = null) : String
        {
            var _loc_4:Array = [];
            if (param3 != null)
            {
                _loc_4 = _loc_4.concat(param3);
            }
            _loc_4.unshift(param2.aid);
            _loc_4.unshift(param2.orderPos);
            _loc_4.unshift(param2.mtype);
            _loc_4.unshift(param2.type);
            _loc_4.unshift(param1);
            _loc_4.push(new Date().time);
            return _loc_4.join("|");
        }// end function

        protected function sendLog(param1:String = "", param2:int = 1) : void
        {
            var _loc_3:* = FastTudouNew.inst;
            var _loc_4:* = new URLVariables();
            new URLVariables().pvid = baseInfo.pvid;
            _loc_4.ocvid = baseInfo.rid;
            _loc_4.cvid = baseInfo.cvid;
            _loc_4.juid = baseInfo.juid;
            _loc_4.iid = baseInfo.iid;
            _loc_4.srcid = baseInfo.sourceId;
            _loc_4.fast = _loc_3.version;
            _loc_4.ppid = baseInfo.ppid;
            _loc_4.pcnt = _pcnt + 1;
            _loc_4.player = baseInfo.playerName;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            switch(param2)
            {
                case 1:
                {
                    _loc_5 = _pscnt + 1;
                    break;
                }
                case 2:
                {
                    _loc_6 = _adcnt + 1;
                    break;
                }
                case 3:
                {
                    _loc_7 = _prcnt + 1;
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            _loc_4.pscnt = _loc_5;
            _loc_4.adcnt = _loc_6;
            _loc_4.prcnt = _loc_7;
            _loc_4.log = param1;
            var _loc_8:* = new URLRequest(Config.AOP_URL);
            new URLRequest(Config.AOP_URL).method = URLRequestMethod.POST;
            _loc_8.data = _loc_4;
            sendToURL(_loc_8);
            nodeLog = null;
            return;
        }// end function

        public static function getInst() : AOPTracker
        {
            if (instance == null)
            {
                lock = false;
                instance = new AOPTracker;
                lock = true;
            }
            return instance;
        }// end function

    }
}
